package com.zhcs.cashbook.util;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

//根据edittext是否为空，判断按钮是否可以点击
public class EditsWatchUtil {
    private List<EditText> editTextList = new ArrayList<>();
    private Button btn;
    private Context context;
    private String textNull, textFull;
    private Integer colorNull, colorFull;
    private Integer bgResourceNull, bgResourceFull;

    //实例化
    public static EditsWatchUtil getInstance() {
        return new EditsWatchUtil();
    }

    //绑定button
    public EditsWatchUtil addButton(Button btn) {
        this.btn = btn;
        btn.setClickable(false);
        return this;
    }

    //绑定edittext
    public EditsWatchUtil addEdittext(EditText et) {
        editTextList.add(et);
        return this;
    }

    //设置Null颜色
    public EditsWatchUtil setNullBgResource(Integer bgResource) {
        this.bgResourceNull = bgResource;
        return this;
    }

    public EditsWatchUtil setNullColor(Integer NullColor) {
        this.colorNull = NullColor;
        return this;
    }

    //设置Full颜色
    public EditsWatchUtil setFullBgResource(Integer bgResource) {
        this.bgResourceFull = bgResource;
        return this;
    }

    public EditsWatchUtil setFullColor(Integer FullColor) {
        this.colorFull = FullColor;
        return this;
    }

    //button设置字体
    public EditsWatchUtil setText(String txt) {
        btn.setText(txt);
        return this;
    }

    //edittext为null时，设置字体
    public EditsWatchUtil setTextNull(String txtNull) {
        this.textNull = txtNull;
        return this;
    }

    //edittext为full时，设置字体
    public EditsWatchUtil setTextFull(String txtFull) {
        this.textFull = txtFull;
        return this;
    }

    //添加Context对象
    public EditsWatchUtil setContext(Context context) {
        this.context = context;
        return this;
    }

    //创建
    public void build() {
        setWatcher();
    }
    //示例
    //EditsWatchUtil.getInstance().addEdittext().addEdittext().addButton().setText().build();

    private void setWatcher() {
        for (int i = 0; i < editTextList.size(); i++) {
            editTextList.get(i).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 0) {
                        setBtnEnable();
                        return;
                    }
                    boolean tag = false;
                    for (int j = 0; j < editTextList.size(); j++) {
                        if (editTextList.get(j).getText().length() != 0) {
                            tag = true;
                        } else {
                            tag = false;
                            break;
                        }
                    }
                    if (tag) {
                        setBtnAble();
                    } else {
                        setBtnEnable();
                    }
                }
            });
        }
    }

    private void setBtnAble() {
        //颜色
        if (colorFull != null) {
            btn.setBackgroundColor(colorFull);
        }
        //字体
        if (!TextUtils.isEmpty(textFull)) {
            btn.setText(textFull);
        }
        //背景
        if (bgResourceFull != null) {
            btn.setBackgroundResource(bgResourceFull);
        }
        btn.setEnabled(true);
    }

    private void setBtnEnable() {
        //颜色
        if (colorNull != null) {
            btn.setBackgroundColor(colorNull);
        }
        //字体
        if (!TextUtils.isEmpty(textNull)) {
            btn.setText(textNull);
        }
        //背景
        if (bgResourceNull != null) {
            btn.setBackgroundResource(bgResourceNull);
        }
        btn.setEnabled(false);
    }

}



